﻿using System;
using static CommonModel.Model.DataTypeDll;

namespace CommonModel.Util
{
    /// <summary>
    /// 数据转换
    /// </summary>
    public class BytesParseToTypeString
    {
        /// <summary>
        /// 由于源端发送的数据都是 LittleEndian 此方法中默认优先以 LittleEndian 解析
        /// 即 float 以 floatEndian 解析
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ParseValue(DataType type, byte[] data)
        {
            if (null == data || data.Length == 0)
            {
                return "";
            }
            string valueStr = "";
            int valueInt;

            try
            {
                switch (type)
                {
                    // 1 字节
                    case DataType.BIT:
                        //valueInt = TransForm.ByteToUint8(data);
                        //valueStr = valueInt.ToString();

                        valueInt = Convert.ToInt32(string.Join("", data), 2);
                        valueStr = valueInt.ToString();
                        break;
                    case DataType.BITARRAY:
                        valueStr = TransForm.ByteToString(data);
                        break;
                    case DataType.BYTE:
                        valueStr = TransForm.ByteToUint8(data).ToString();
                        break;
                    case DataType.CHAR:
                        valueStr = TransForm.ByteToIntBigEndian(data).ToString();
                        break;
                    case DataType.UCHAR:
                        valueStr = TransForm.ByteToUint8(data).ToString();
                        break;
                    case DataType.BOOL:
                        valueInt = TransForm.ByteToUint8(data);
                        valueStr = valueInt.ToString();
                        break;
                    case DataType.ENUM:
                        valueInt = TransForm.ByteToUint8(data);
                        valueStr = valueInt.ToString();
                        break;
                    case DataType.UINT8:
                        valueStr = TransForm.ByteToUint8(data).ToString();
                        break;
                    case DataType.INT8:
                        valueStr = ((sbyte)data[0]).ToString();
                        break;
                    // 2 字节
                    case DataType.UINT16:
                        if (data.Length == 1)
                        {
                            valueStr = TransForm.ByteToUint8(data).ToString();
                        }
                        else
                        { 
                            valueStr = TransForm.ByteToUint16(data).ToString();
                        }
                        break;
                    case DataType.UINT16REF:
                        valueStr = TransForm.ByteToUint16BigEndian(data).ToString();
                        break;
                    case DataType.SHORT:
                        valueStr = TransForm.ByteToShort(data).ToString();
                        break;
                    case DataType.SHORTREF:
                        valueStr = TransForm.ByteToShortBigEndian(data).ToString();
                        break;
                    //case DataType.CHL11:
                    //    valueStr = TransForm.bytesToFreChlIntBigEndian(data).ToString();
                    //    break;
                    //case DataType.CHL12:
                    //    valueStr = TransForm.bytesToFreChlInt(data).ToString();
                    //    break;
                    // 4 字节
                    case DataType.INT:
                        valueStr = TransForm.ByteToInt(data).ToString();
                        break;
                    case DataType.INTREF:
                        valueStr = TransForm.ByteToIntBigEndian(data).ToString();
                        break;
                    case DataType.UINT:
                        valueStr = TransForm.ByteToUint32(data).ToString();
                        break;
                    case DataType.UINTREF:
                        valueStr = TransForm.ByteToUint32BigEndian(data).ToString();
                        break;
                    case DataType.FLOAT:
                        //if (data.Length == 1)
                        //{
                        //    valueStr = data[0].ToString() + ".000";
                        //}
                        //else
                        //{
                        //    valueStr = string.Format("{0:0.000}", TransForm.ByteToFloat(data));//结果是0.000的形式
                        //}
                        //长度补足
                        byte[] tmpFloat = data.Length < 4 ? new byte[4 - data.Length].AddBytes(data) : data;
                        //valueStr = TransForm.ByteToFloat(tmpFloat).ToString();
                        valueStr = TransForm.IsNanInfinity(TransForm.ByteToFloat(tmpFloat)).ToString();
                        break;
                    case DataType.FLOATREF:
                        //if (data.Length == 1)
                        //{
                        //    valueStr = data[0].ToString() + ".000";
                        //}
                        //else
                        //{
                        //    valueStr = string.Format("{0:0.000}", TransForm.ByteToFloatBigEndian(data));//结果是0.000的形式
                        //}
                        //长度补足
                        byte[] tmpFloatRef = data.Length < 4 ? data.AddBytes(new byte[4 - data.Length]) : data;
                        //valueStr = TransForm.ByteToFloatBigEndian(tmpFloatRef).ToString();
                        valueStr = TransForm.IsNanInfinity(TransForm.ByteToFloatBigEndian(tmpFloatRef)).ToString();
                        break;
                    case DataType.UINT32:
                        valueStr = TransForm.ByteToUint32(data).ToString();
                        break;
                    case DataType.UINT32REF:
                        valueStr = TransForm.ByteToUint32BigEndian(data).ToString();
                        break;
                    //高位到低位解 20220805 ysw
                    case DataType.UINT32ASC:
                        valueStr = TransForm.ByteArrayToInt(data, true, false).ToString();
                        break;
                    //低位到高位解 20220805 ysw
                    case DataType.UINT32DESC:
                        valueStr = TransForm.ByteArrayToInt(data, false, false).ToString();
                        break;
                    case DataType.IPV4:
                        valueStr = TransForm.ByteToIp(data);
                        break;
                    case DataType.IPV4_4:
                        valueStr = TransForm.FourByteToIp(data);
                        break;
                    case DataType.UINT_IP:
                        valueStr = TransForm.FourByteToIp(data);
                        break;
                    // 8 字节
                    case DataType.LONG:
                        //添加长度判断 20230417 ysw
                        byte[] tmpLong = data.Length < 8 ? data.AddBytes(new byte[8 - data.Length]) : data;
                        valueStr = TransForm.ByteToLong(tmpLong).ToString();
                        break;
                    case DataType.LONGREF:
                        valueStr = TransForm.ByteToLongBigEndian(data).ToString();
                        break;
                    case DataType.DOUBLE:
                        valueStr = TransForm.ByteToDouble(data).ToString();
                        break;
                    case DataType.DOUBLEREF:
                        valueStr = TransForm.ByteToDoubleBigEndian(data).ToString();
                        break;
                    // 特殊 类型
                    case DataType.DATE:
                        valueStr = TransForm.ByteToDate(data).ToString();
                        break;
                    case DataType.DATESHORT:
                        valueStr = TransForm.ByteToDateShort(data).ToString();
                        break;
                    case DataType.BITOP:
                        valueStr = TransForm.bytesToBitOp(data).ToString();
                        break;
                    case DataType.BYTEOP:
                        valueStr = TransForm.bytesToByteOp(data).ToString();
                        break;
                    case DataType.USHORTOP:
                        valueStr = TransForm.bytesToUShortOp(data).ToString();
                        break;
                    case DataType.SINGLEOP:
                        valueStr = TransForm.bytesToSingleOp(data).ToString();
                        break;
                    case DataType.SCHEDULE:
                        valueStr = TransForm.bytesToSchedule(data).ToString();
                        break;
                    case DataType.AUTOMSGCFG:
                        valueStr = TransForm.bytesToAutoMsgCfg(data).ToString();
                        break;
                    case DataType.CONTREAD:
                        valueStr = TransForm.bytesToContRead(data).ToString();
                        break;
                    case DataType.MAC:
                        valueStr = TransForm.bytesToMac(data).ToString();
                        break;
                    case DataType.MultipathData:// 多径数据
                        valueStr = TransForm.bytesToMultipathData(data).ToString();
                        break;
                    // n 字节
                    case DataType.STRUCT:
                        break;
                    case DataType.STRING:
                        valueStr = TransForm.ByteToStringUTF8(data);
                        break;
                    case DataType.HEXSTR:
                        // DecimalFormat decimalFormat = new DecimalFormat("#0.000");
                        //valueStr = TransForm.ByteToHexStringBigEndian(data);
                        //20230329 ysw 修改
                        valueStr = TransForm.byteToHex(data, false);
                        break;
                    case DataType.HEXSTRREF:
                        // DecimalFormat decimalFormat = new DecimalFormat("#0.000");
                        //valueStr = TransForm.ByteToHexStringBigEndian(data);//大端
                        //20230329 ysw 修改
                        valueStr = TransForm.byteToHex(data, true);
                        break;
                    case DataType.UNKOWN:
                        //valueStr = "占位";
                        valueStr = "";
                        break;
                    default:
                        valueStr = "";
                        break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: {0} data=[" + TransForm.byteToHex(data) + "] type=[" + type.ToString() + "]", e);
            }
            return valueStr;
        }
    }
}
